<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人脸识别分析系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px 20px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2em;
            margin-bottom: 8px;
        }
        
        .header p {
            font-size: 1em;
            opacity: 0.9;
        }
        
        .main-content {
            padding: 25px;
        }
        
        .upload-section {
            background: #f8f9ff;
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .upload-box {
            border: 3px dashed #667eea;
            border-radius: 12px;
            padding: 40px 30px;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
            background: white;
        }
        
        .upload-box:hover {
            background: #f0f2ff;
            border-color: #764ba2;
            transform: translateY(-2px);
        }
        
        .upload-icon {
            font-size: 3em;
            color: #667eea;
            margin-bottom: 15px;
        }
        
        .file-input {
            display: none;
        }
        
        .upload-text {
            font-size: 1.2em;
            color: #333;
            margin-bottom: 10px;
        }
        
        .upload-subtext {
            color: #666;
            font-size: 0.9em;
            margin-bottom: 20px;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 10px 25px;
            border-radius: 25px;
            font-size: 1em;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .results-section {
            display: none;
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-top: 20px;
        }
        
        .results-section.show {
            display: block;
        }
        
        .success-message {
            background: #e8f5e8;
            color: #2e7d32;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
            margin-bottom: 20px;
        }
        
        .results-container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            height: 450px;
        }
        
        .image-container {
            background: white;
            border-radius: 10px;
            padding: 15px;
            border: 2px solid #e0e0e0;
            height: 100%;
        }
        
        .image-box {
            height: 100%;
            display: flex;
            flex-direction: column;
        }
        
        .image-box img {
            width: 100%;
            height: calc(100% - 30px);
            object-fit: contain;
            border-radius: 8px;
        }
        
        .image-label {
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
            font-size: 1.1em;
            height: 30px;
            display: flex;
            align-items: center;
        }
        
        .analysis-sidebar {
            display: flex;
            flex-direction: column;
            gap: 15px;
            height: 100%;
        }
        
        .summary-stats {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 12px;
            height: 80px;
        }
        
        .stat-card {
            background: white;
            padding: 12px;
            border-radius: 8px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border-left: 4px solid #667eea;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        
        .stat-value {
            font-size: 1.4em;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #666;
            font-size: 0.8em;
        }
        
        .face-list-container {
            background: #f8f9ff;
            border-radius: 10px;
            padding: 15px;
            flex: 1;
            min-height: 0;
            display: flex;
            flex-direction: column;
        }
        
        .face-list-title {
            font-weight: bold;
            color: #667eea;
            margin-bottom: 10px;
            font-size: 1em;
            height: 20px;
        }
        
        .face-list {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 12px;
            flex: 1;
            overflow-y: auto;
            padding-right: 8px;
            min-height: 0;
        }
        
        .face-list::-webkit-scrollbar {
            width: 5px;
        }
        
        .face-list::-webkit-scrollbar-thumb {
            background: #667eea;
            border-radius: 3px;
        }
        
        .face-item {
            background: white;
            border-radius: 8px;
            padding: 12px;
            box-shadow: 0 2px 6px rgba(0,0,0,0.1);
            border-left: 4px solid #667eea;
            min-height: 150px;
        }
        
        .face-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            padding-bottom: 6px;
            border-bottom: 1px solid #eee;
        }
        
        .face-id {
            background: #667eea;
            color: white;
            padding: 4px 8px;
            border-radius: 12px;
            font-weight: bold;
            font-size: 0.8em;
        }
        
        .detail-section {
            margin-bottom: 8px;
        }
        
        .section-title-small {
            font-weight: bold;
            color: #667eea;
            margin-bottom: 6px;
            font-size: 0.85em;
        }
        
        /* 纵向布局样式 */
        .vertical-layout {
            display: flex;
            flex-direction: column;
            gap: 4px;
        }
        
        .vertical-item {
            display: flex;
            justify-content: space-between;
            padding: 3px 0;
            font-size: 0.8em;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .vertical-item:last-child {
            border-bottom: none;
        }
        
        .detail-label {
            color: #666;
        }
        
        .detail-value {
            font-weight: bold;
            color: #333;
        }
        
        /* 特征标签样式 */
        .feature-tag {
            background: #e3f2fd;
            color: #1976d2;
            padding: 3px 6px;
            border-radius: 10px;
            font-size: 0.75em;
            text-align: center;
        }
        
        .feature-tag.true {
            background: #e8f5e8;
            color: #2e7d32;
        }
        
        .feature-tag.false {
            background: #ffebee;
            color: #c62828;
        }
        
        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
            margin: 15px 0;
            border-left: 4px solid #c62828;
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 15px;
            margin-top: 20px;
        }
        
        .feature {
            text-align: center;
            padding: 15px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 6px rgba(0,0,0,0.1);
        }
        
        .feature-icon {
            font-size: 1.8em;
            color: #667eea;
            margin-bottom: 8px;
        }
        
        .feature h3 {
            margin-bottom: 5px;
            color: #333;
            font-size: 0.9em;
        }
        
        .feature p {
            color: #666;
            font-size: 0.8em;
            line-height: 1.3;
        }
        
        .footer {
            text-align: center;
            padding: 15px;
            color: #666;
            border-top: 1px solid #eee;
            margin-top: 20px;
        }
        
        .new-analysis-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 8px 20px;
            border-radius: 20px;
            font-size: 0.9em;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 15px;
        }
        
        .loading-spinner {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 35px;
            height: 35px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>👤 智能人脸分析系统</h1>
            <p>上传图片，实时分析人脸信息</p>
        </div>
        
        <div class="main-content">
            <!-- 上传区域 -->
            <div class="upload-section" id="uploadSection">
                <form action="/" method="post" enctype="multipart/form-data" id="uploadForm">
                    <div class="upload-box" id="uploadBox" onclick="document.getElementById('fileInput').click()">
                        <div class="upload-icon">📁</div>
                        <div class="upload-text">点击选择图片或拖拽到此处</div>
                        <div class="upload-subtext">
                            支持格式: JPG, PNG, JPEG, GIF
                        </div>
                        <input type="file" id="fileInput" name="file" class="file-input" accept="image/*">
                        <button type="button" class="btn" id="selectButton">
                            选择图片
                        </button>
                    </div>
                </form>
            </div>

            <!-- 加载动画 -->
            <div class="loading-spinner" id="loadingSpinner">
                <div class="spinner"></div>
                <p>正在分析图片，请稍候...</p>
            </div>

            <!-- 错误信息 -->
            {% if error %}
            <div class="error-message">
                <h3>❌ 分析失败</h3>
                <p>{{ error }}</p>
            </div>
            {% endif %}

            <!-- 分析结果 -->
            {% if analysis_complete %}
            <div class="results-section show" id="resultsSection">
                <div class="success-message">
                    <h3>✅ 分析完成！</h3>
                    <p>成功检测到 {{ face_count }} 张人脸</p>
                </div>

                <div class="results-container">
                    <!-- 分析结果图片 -->
                    <div class="image-container">
                        <div class="image-box">
                            <span class="image-label">🔍 分析结果</span>
                            <img src="{{ url_for('static', filename='uploads/' + processed_image) }}" alt="人脸分析结果">
                        </div>
                    </div>
                    
                    <!-- 分析侧边栏 -->
                    <div class="analysis-sidebar">
                        <!-- 汇总统计 -->
                        <div class="summary-stats">
                            <div class="stat-card">
                                <div class="stat-value">{{ face_count }}</div>
                                <div class="stat-label">总人脸数</div>
                            </div>
                            {% if face_count > 0 %}
                            <div class="stat-card">
                                <div class="stat-value">
                                    {% set male_count = face_info|selectattr('gender.result', 'equalto', 'Male')|list|length %}
                                    {{ male_count }}
                                </div>
                                <div class="stat-label">男性</div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value">
                                    {% set female_count = face_info|selectattr('gender.result', 'equalto', 'Female')|list|length %}
                                    {{ female_count }}
                                </div>
                                <div class="stat-label">女性</div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value">
                                    {% set smile_count = face_info|selectattr('features.smile_detected', 'equalto', true)|list|length %}
                                    {{ smile_count }}
                                </div>
                                <div class="stat-label">微笑检测</div>
                            </div>
                            {% endif %}
                        </div>

                        <!-- 人脸列表 -->
                        <div class="face-list-container">
                            <div class="face-list-title">详细分析结果</div>
                            <div class="face-list">
                                {% for face in face_info %}
                                <div class="face-item">
                                    <div class="face-header">
                                        <h3 style="font-size: 0.9em; margin: 0;">人脸 {{ face.face_id }}</h3>
                                        <span class="face-id">ID: {{ face.face_id }}</span>
                                    </div>
                                    
                                    <!-- 基本信息 - 纵向分布 -->
                                    <div class="detail-section">
                                        <div class="section-title-small">📍 基本信息</div>
                                        <div class="vertical-layout">
                                            <div class="vertical-item">
                                                <span class="detail-label">位置:</span>
                                                <span class="detail-value">({{ face.position.x }}, {{ face.position.y }})</span>
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">宽度:</span>
                                                <span class="detail-value">{{ face.position.width }}px</span>
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">高度:</span>
                                                <span class="detail-value">{{ face.position.height }}px</span>
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">占图比例:</span>
                                                <span class="detail-value">{{ face.size_ratio }}%</span>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- 属性分析 - 纵向分布 -->
                                    <div class="detail-section">
                                        <div class="section-title-small">👤 属性分析</div>
                                        <div class="vertical-layout">
                                            <div class="vertical-item">
                                                <span class="detail-label">性别:</span>
                                                <span class="detail-value">{{ face.gender.result }}</span>
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">年龄:</span>
                                                <span class="detail-value">{{ face.age.result }}</span>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- 特征检测 - 纵向分布 -->
                                    <div class="detail-section">
                                        <div class="section-title-small">🔍 特征检测</div>
                                        <div class="vertical-layout">
                                            <div class="vertical-item">
                                                <span class="detail-label">眼睛数量:</span>
                                                <span class="detail-value">  {{ face.features.eye_count }} </span>
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">微笑检测:</span>
                                                <span class="detail-value">{{ face.features.smile_detected }} </span>                               
                                            </div>
                                            <div class="vertical-item">
                                                <span class="detail-label">脸部朝向:</span>
                                                <span class="detail-value">{{ face.features.orientation }}</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

        
        </div>
    </div>

    <script>
        function resetAnalysis() {
            document.getElementById('resultsSection').classList.remove('show');
            document.getElementById('uploadSection').style.display = 'block';
            document.getElementById('fileInput').value = '';
            window.scrollTo({ top: 0, behavior: 'smooth' });
        }

        document.getElementById('fileInput').addEventListener('change', function() {
            if (this.files.length > 0) {
                document.getElementById('loadingSpinner').style.display = 'block';
                document.getElementById('uploadSection').style.display = 'none';
                document.getElementById('uploadForm').submit();
            }
        });

        document.getElementById('selectButton').addEventListener('click', function(e) {
            e.stopPropagation(); // 阻止事件冒泡
            document.getElementById('fileInput').click();
        });

        const uploadBox = document.getElementById('uploadBox');
        
        uploadBox.addEventListener('dragover', function(e) {
            e.preventDefault();
            this.classList.add('drag-over');
        });
        
        uploadBox.addEventListener('dragleave', function(e) {
            e.preventDefault();
            this.classList.remove('drag-over');
        });
        
        uploadBox.addEventListener('drop', function(e) {
            e.preventDefault();
            this.classList.remove('drag-over');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                document.getElementById('fileInput').files = files;
                document.getElementById('loadingSpinner').style.display = 'block';
                document.getElementById('uploadSection').style.display = 'none';
                document.getElementById('uploadForm').submit();
            }
        });

        // 如果有分析结果，自动滚动到结果区域
        setTimeout(() => {
            document.getElementById('resultsSection').scrollIntoView({ 
                behavior: 'smooth' 
            });
        }, 500);
    </script>
</body>
</html>